package com.doctor.windflower_doctor.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.activity.QuestionOnLineActivity;
import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * <p>
 * 功能 提问记录适配器
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年11月6日 下午3:59:34
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class HistoryAskingAdapter extends BaseAdapter implements Constent {
	private Context context;
	private BitmapDisplayConfig configImage;
	private List<QuestionBeen> questionBeens;
	private DisplayImageOptions options;

	public HistoryAskingAdapter(Context context) {
		this.context = context;
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
		configImage = new BitmapDisplayConfig();
		Drawable loadingDrawable = context.getResources().getDrawable(R.drawable.loading_img);
		Drawable loadingDrawableImage = context.getResources().getDrawable(R.drawable.list_default_icon);
		configImage.setLoadingDrawable(loadingDrawable);
		configImage.setLoadFailedDrawable(loadingDrawableImage);
		questionBeens = new ArrayList<QuestionBeen>();
	}

	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		return questionBeens.size();

	}

	@Override
	public Object getItem(int position) {

		// TODO Auto-generated method stub
		return questionBeens.get(position);

	}

	@Override
	public long getItemId(int position) {

		// TODO Auto-generated method stub
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_history_asking, null);
			holder.image_url = (RoundedImageView) convertView.findViewById(R.id.image_url);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.question_text = (TextView) convertView.findViewById(R.id.question_text);
			holder.create_time = (TextView) convertView.findViewById(R.id.create_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final QuestionBeen questionBeen = questionBeens.get(position);
		SystemApplication.getInstance().mImageLoad1.displayImage(questionBeen.imgUrl, holder.image_url, options);
		holder.name.setText(questionBeen.nick);
		holder.question_text.setText(questionBeen.text);
		holder.create_time.setText(questionBeen.createTime);
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, QuestionOnLineActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("replyStatus", 3);
				bundle.putString(QUESTIONID, questionBeen.questionId);
				bundle.putString(USERID, questionBeen.userId);
				bundle.putString(NAME, questionBeen.nick);
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});
		return convertView;

	}

	public void addListData(List<QuestionBeen> questionBeens) {
		if (questionBeens != null) {
			this.questionBeens.addAll(questionBeens);
			notifyDataSetChanged();
		}
	}

	public void clearData() {
		if (questionBeens != null) {
			this.questionBeens.clear();
			notifyDataSetChanged();
		}
	}

	class ViewHolder {
		RoundedImageView image_url;
		TextView name;
		TextView question_text;
		TextView create_time;
	}
}
